#include<bits/stdc++.h>
using namespace std;
typedef pair<int , int>PII;
map<string , PII>mp;
string s;
int n , k , ben , num;
long long ans = 0;
int main(){
	freopen("maxbenefit.in" , "r" , stdin);
	freopen("maxbenefit.out" , "w" , stdout);
	cin >> n >> k;
	while(n--){
		cin >> s >> ben >> num;
		mp[s] = {ben , num};
	}
	for(int i = 1 ; i <= k ; i++){
		cin >> s >> num;
		(mp[s]).second -= num;
		ans += 1ll * mp[s].first * num;
		if(mp[s].second < 0){
			cout << -i << endl;
			return 0;
		}
	}
	cout << ans << endl;
	return 0;
}
